package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.ManagerDao;
import com.qf.dao.PatientDao;
import com.qf.model.entity.OutConsumption;
import com.qf.dao.OutConsumptionDao;
import com.qf.model.entity.Patient;
import com.qf.model.req.OutConsumptionAndPatientReq;
import com.qf.model.vo.OutConsumptionVO;
import com.qf.service.OutConsumptionService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * (OutConsumption)表服务实现类
 *
 * @author ZhiHuiJun
 * @since 2024-04-30 09:30:48
 */
@Service("outConsumptionService")
public class OutConsumptionServiceImpl implements OutConsumptionService {

    @Autowired
    private OutConsumptionDao outConsumptionDao;

    @Autowired
    private ManagerDao managerDao;

    @Autowired
    private PatientDao patientDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OutConsumption queryById(Integer id) {
        return this.outConsumptionDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<OutConsumption> queryByPage(Integer pageNum) {
        return this.outConsumptionDao.queryAllByLimit();
    }

    /**
     * 新增数据
     *
     * @param outConsumptionAndPatientReq 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(OutConsumptionAndPatientReq outConsumptionAndPatientReq) {
        Patient patient = new Patient();
        patient.setPatientName(outConsumptionAndPatientReq.getPatientName());
        if (Objects.nonNull(outConsumptionAndPatientReq.getCard())){
            patient.setCard(outConsumptionAndPatientReq.getCard());
        }
        patientDao.insertSelective(patient);
        OutConsumption outConsumption = new OutConsumption();
        outConsumption.setOrderNumber(outConsumptionAndPatientReq.getOrderNumber());
        outConsumption.setSource(outConsumptionAndPatientReq.getSource());
        outConsumption.setBusinessType(outConsumptionAndPatientReq.getBusinessType());
        outConsumption.setCreateTime(new Date());
        outConsumption.setExpenseType(outConsumptionAndPatientReq.getExpenseType());
        outConsumption.setPaymentMethod(outConsumptionAndPatientReq.getPaymentMethod());
        outConsumption.setAmountPaid(outConsumptionAndPatientReq.getAmountPaid());
        outConsumption.setPatientId(patient.getPatientId().intValue());
        int i = outConsumptionDao.insert(outConsumption);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param outConsumption 实例对象
     * @return 实例对象
     */
    @Override
    public int update(OutConsumption outConsumption) {
       int i = outConsumptionDao.update(outConsumption);
       if (i>0){
           return 1;
       }
       return -1;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.outConsumptionDao.deleteById(id) > 0;
    }

    @Override
    public PageInfo<OutConsumptionVO> selectConsumptionByCondition(Integer pageNum, Integer pageSize, String patientName, String orderNumber ,String card) {
        PageHelper.startPage(pageNum, pageSize);
        List<OutConsumptionVO> list = outConsumptionDao.selectConsumptionByCondition(patientName,orderNumber,card);
        return new PageInfo<>(list);
//        Manager manager = managerDao.queryById(1L);
//        List<TestVO> lastList = new ArrayList<>();
//        consumptionPageInfo.getList().forEach(item ->{
//            TestVO testVO = new TestVO();
//            testVO.setManagerId(Long.valueOf(item.getId()));
//            testVO.setOrderNumber(item.getOrderNumber());
//            if (ObjectUtils.nullSafeEquals(item.getId(),manager.getMgrId())){
//                testVO.setMgrName(manager.getMgrName());
//            }
//            lastList.add(testVO);
//        });
//        PageInfo<TestVO> pageInfo = new PageInfo<>();
//        pageInfo.setList(lastList);
//        return pageInfo;
    }

    @Override
    public boolean delBatch(List<Integer> list) {
        if (!CollectionUtils.isEmpty(list)){
            int i = outConsumptionDao.delBatch(list);
            if (i > 0) {
                return true;
            }
        }
        return false;
    }
}
